### PACKAGE PREAMBLE ###

# Create a vector of package names
packages <- c("dplyr", "ggplot2", "tidyr", "tidyverse", "data.table", "readxl", "foreign", "devtools", "fixest", "sandwich", "modelsummary", "kableExtra")

installed_packages <- packages %in% rownames(installed.packages())
if (any(installed_packages == FALSE)) {
  install.packages(packages[!installed_packages])
}

# Packages loading
invisible(lapply(packages, library, character.only = TRUE))
rm(packages, installed_packages)

options(modelsummary_format_numeric_latex = "plain")
f <- function(x) format(x, digits = 3, nsmall = 2, scientific = FALSE)

getwd()
setwd(dir = "/Users/jmc4qg/The Lab Dropbox/Jonathan Colmer/ShotSpotter_env/Journal_submissions/ReStat/Replication Folder/")

mydata_UCR <- read.dta("Analysis Data/UCR_analysis.dta")
mydata_UCR_NIBRS <- filter(mydata_UCR, NIBRS == 1)

DiT_1_UCR_NIBRS <- feols(homicide_pc~ temp_MP + prec_MP | year + sample_month[tMean] +sample_month[MP] + sample_month[prec] + month , data=mydata_UCR_NIBRS)
summary(DiT_1_UCR_NIBRS, cluster="STATE")
DiT_1_UCR <- feols(homicide_pc~ temp_MP + prec_MP | year + sample_month[tMean] +sample_month[MP] + sample_month[prec] + month , data=mydata_UCR)
summary(DiT_1_UCR, cluster="STATE")

DiDiT_3_UCR_NIBRS <- feols(homicide_pc~ temp_MP + prec_MP | ori_year + ORI[tMean]+sample_month[tMean]+ORI[prec]+sample_month[prec], data=mydata_UCR_NIBRS)
summary(DiDiT_3_UCR_NIBRS, cluster="STATE")
DiDiT_3_UCR <- feols(homicide_pc~ temp_MP + prec_MP | ori_year+ORI[tMean]+sample_month[tMean]+ORI[prec]+sample_month[prec], data=mydata_UCR)
summary(DiDiT_3_UCR, cluster="STATE")

models <- list(DiT_1_UCR_NIBRS, DiT_1_UCR, DiDiT_3_UCR_NIBRS, DiDiT_3_UCR)

modelsummary(models, vcov = ~STATE, output = "Figures and Tables/Table_B6/Table_B6.tex", fmt = f, stars = c('*' = 0.1, '**' = 0.05, '***' = 0.01))
